package com.ruoyi.exam.controller;

import java.util.List;
import java.util.stream.Collectors;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exam.domain.entity.TTaskExe;
import com.ruoyi.exam.service.ITTaskExeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务执行列Controller
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/exam/exe")
public class TTaskExeController extends BaseController {

    @Autowired
    private ITTaskExeService tTaskExeService;

    /**
     * 查询任务执行列列表
     */
    @PreAuthorize("@ss.hasPermi('exam:exe:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTaskExe tTaskExe, Integer pageNum, Integer pageSize) {

        List<TTaskExe> list = tTaskExeService.selectTTaskExeList(tTaskExe);
        System.out.println("任务列表个数"+list.size());
        // 根据tTaskExe的taskName属性过滤list
        if (tTaskExe.getTaskName() != null && !tTaskExe.getTaskName().isEmpty()) {
            list = list.stream().filter(exe -> exe.getTask().getName().contains(tTaskExe.getTaskName()))
                    .collect(Collectors.toList());
        }

        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 9999 : pageSize;
        int total = list.size();

        int startIndex = (pageNum - 1) * pageSize;
        int endIndex = Math.min(startIndex + pageSize, total);

        List<TTaskExe> pageList = startIndex < total ? list.subList(startIndex, endIndex) : new ArrayList<>();

        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(200);
        rspData.setRows(pageList);
        rspData.setTotal(total);
        rspData.setMsg("查询成功");

        return rspData;
    }

    /**
     * 查询任务执行列列表
     */
    @PreAuthorize("@ss.hasPermi('exam:score:list')")
    @GetMapping("/detail/list")
    public TableDataInfo detailList(TTaskExe tTaskExe) {
        startPage();
        List<TTaskExe> list = tTaskExeService.selectTTaskExeDetailList(tTaskExe);
        return getDataTable(list);
    }

    /**
     * 导出任务执行列列表
     */
    @PreAuthorize("@ss.hasPermi('exam:exe:export')")
    @Log(title = "任务执行列", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTaskExe tTaskExe) {
        List<TTaskExe> list = tTaskExeService.selectTTaskExeList(tTaskExe);
        ExcelUtil<TTaskExe> util = new ExcelUtil<TTaskExe>(TTaskExe.class);
        util.exportExcel(response, list, "任务执行列数据");
    }

    /**
     * 获取任务执行列详细信息
     */
    @PreAuthorize("@ss.hasPermi('exam:exe:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tTaskExeService.selectTTaskExeById(id));
    }

    /**
     * 获取任务执行详细信息，具体到每个人的任务完成情况
     */
    @PreAuthorize("@ss.hasPermi('exam:exe:query')")
    @GetMapping(value = "/detail/{taskId}")
    public AjaxResult getDetail(@PathVariable("taskId") Long taskId) {
        List<TTaskExe> list = tTaskExeService.selectTTaskExeListByTaskId(taskId);
        return success(list);
    }

    /**
     * 新增任务执行列
     */
    @PreAuthorize("@ss.hasPermi('exam:exe:add')")
    @Log(title = "任务执行列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTaskExe tTaskExe) {
        return toAjax(tTaskExeService.insertTTaskExe(tTaskExe));
    }

    /**
     * 修改任务执行列
     */
    @PreAuthorize("@ss.hasPermi('exam:exe:edit')")
    @Log(title = "任务执行列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTaskExe tTaskExe) {
        return toAjax(tTaskExeService.updateTTaskExe(tTaskExe));
    }

    /**
     * 删除任务执行列
     */
    @PreAuthorize("@ss.hasPermi('exam:exe:remove')")
    @Log(title = "任务执行列", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tTaskExeService.deleteTTaskExeByIds(ids));
    }
}
